//
//  OtherClassificationViewController.m
//  EasyToTesco
//
//  Created by mc on 16/1/31.
//  Copyright © 2016年 Jack. All rights reserved.
//  更多分类

#import "OtherClassificationViewController.h"
#import "HeaderFileForEasyToTesco.h"
#import "ButtonView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"

//
#import "ShoeBagDressViewController.h"

@interface OtherClassificationViewController ()

@property (nonatomic, strong)NSMutableArray *nameclassArray;
@property (nonatomic, strong)NSMutableArray *idClassArray;

@end

@implementation OtherClassificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.nameclassArray = [@[]mutableCopy];
    self.idClassArray = [@[]mutableCopy];
    
    
    [self addOtherClassificationUI];
    
    [self otherClassdatasources];
}

- (void)addOtherClassificationUI {
    [self addTopUI];
//    [self addBTnView];
}

- (void)addTopUI {
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    
    self.titleLabel.text = @"更多分类";
}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addBTnView {
//    NSArray *imageArray = @[[UIImage imageNamed:@"wszq-color-icon.png"],
//                            [UIImage imageNamed:@"xbfs-color-icon.png"],
//                            [UIImage imageNamed:@"jdsm-color-icon.png"],
//                            [UIImage imageNamed:@"sycz-color-icon.png"],
//                            [UIImage imageNamed:@"hdzx-color-icon.png"],
//                            [UIImage imageNamed:@"hfcz-color-icon.png"],
//                            [UIImage imageNamed:@"msbj-color-icon.png"],
//                            [UIImage imageNamed:@"xxyl-color-icon.png"]
//                            ];
    NSMutableArray *imageArray = [@[]mutableCopy];
    for (NSString *str in self.nameclassArray) {
        [imageArray addObject:[UIImage imageNamed:@"wszq-color-icon.png"]];
    }
    
//    NSArray *titleArray = @[@"微商专区", @"鞋包服饰", @"家电数码", @"石油充值", @"活动中心", @"话费充值", @"美食保健", @"休闲娱乐"];
    NSInteger k = 0;
    
    NSInteger hangNum = self.nameclassArray.count/4;
    if ((self.nameclassArray.count%4) == 0) {
        hangNum = self.nameclassArray.count/4;
    }else  {
        hangNum = self.nameclassArray.count/4+1;
    }
    
    for (NSInteger i = 0; i < hangNum; i++) {
        for (NSInteger j = 0; j < 4; j++) {
            ButtonView *BtnView = [[ButtonView alloc] initWithFrame:CGRectMake((WEITH/4)*j, CGRectGetMaxY(self.firstView.frame) + (WEITH/4)*i, WEITH/4, WEITH/4)];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = BtnView.frame;
            [button addTarget:self action:@selector(buttonViewClick:) forControlEvents:UIControlEventTouchUpInside];
            BtnView.titleLabel.text = [NSString stringWithFormat:@"%@", self.nameclassArray[k]];
            BtnView.picImage.image = imageArray[k];
            button.tag = 800+k;
            [self.view addSubview:BtnView];
            [self.view addSubview:button];
            k++;
        }
    }
}

- (void)buttonViewClick:(UIButton *)button {
    NSLog(@"其他分类页面%ld", button.tag);
    
    ShoeBagDressViewController *shoebagVC = [[ShoeBagDressViewController alloc] init];
    shoebagVC.titleStr = self.nameclassArray[button.tag - 800];
    shoebagVC.firstid = self.idClassArray[button.tag-800];
    [self presentViewController:shoebagVC animated:YES completion:nil];
    
}


- (void)otherClassdatasources {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
//    NSString *all = @"1";
    
//NSString *nosort = @"1";
//    NSDictionary *param = @{@"all":[NSString stringWithFormat:@"%@", all]};
    
    [mgr GET:@"http://ylgw8.com/Prolist/getClass" parameters:nil  success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        NSMutableArray *array = [self toArrayOrNSDictionary:responseObject];
        for (NSDictionary *dic in array) {
            [self.nameclassArray addObject:dic[@"classname"]];
            [self.idClassArray addObject:[NSString stringWithFormat:@"%@", dic[@"id"]]];
        }
      
    
        [self addBTnView];
       
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
//         NSLog(@"数据请求错误原因:%@", error);
//         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
